<?php

/**
 * 73yc 商品API接口
 * ============================================================================
 * 版权所有 2005-2010 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: yehuaixiao $
 * $Id: order.php 17219 2011-01-27 10:49:19Z yehuaixiao $
 */

define('IN_ECS', true);
require('includes/init.php');
require(ROOT_PATH . 'includes/cls_json.php');
if($_REQUEST['name']!='21232f297a57a5a743894a0e4a801fc3' || $_REQUEST['pass']!='0192023a7bbd73250516f069df18b500'){
    exit;
}

if($_REQUEST['act']=='goods'){
	$json   = new JSON;
	$content=!empty($_REQUEST['content'])?$_REQUEST['content']:'';
	$area_id=!empty($_SESSION['area_id'])?$_SESSION['area_id']:1;
  	$arr = array();
        /* 检查关键字中是否存在于词库 */
        $arr        = sc_word($content);
        $arr        = $arr ? array_values($arr) : array($content);
        $operator   = " OR ";
        $keyword_arr = $arr;
        $keywords = 'AND (';
        $goods_ids = array();
        foreach ($arr AS $key => $val)
        {
            $val        = mysql_like_quote(trim($val));
            $sc_dsad    = $_REQUEST['sc_ds'] ? " OR goods_desc LIKE '%$val%'" : '';
            if($key != 0) $keywords .= " AND ";
            $keywords .= " CONCAT(goods_name,keywords,goods_desc) LIKE '%$val%' ";
            $db->autoReplace($ecs->table('keywords'), array('date' => local_date('Y-m-d'),
                'searchengine' => 'ecshop', 'keyword' => addslashes(str_replace('%', '', $val)), 'count' => 1), array('count' => 1));
        }

        $keywords .= ") OR keywords LIKE '%".$content."%' ";
        //获取符合条件的商品
        $sql = 'SELECT g.goods_id, g.goods_name,g.goods_thumb ' . 
        "FROM " . $GLOBALS['ecs']->table('goods'). " AS g ". 
        "WHERE g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 ".
        $keywords ;
        $goods=$db->getall($sql);
        shuffle($goods);
        $goods =array_slice($goods,0,5);
    	echo $json->encode($goods);
}





// 返回分词函数
function sc_word($keywords)
{
        $word = word('word');
        if($keywords){
            for($i=0;$i<mb_strlen($keywords,'UTF8');$i++){
                for ($j=$i+1; $j <= mb_strlen($keywords,'UTF8'); $j++) { 
                    $k = mb_substr($keywords,$i,$j-$i,'UTF8');
                    if(in_array($k,$word)){
                        $word_arr[] = $k;
                    }
                }
            }
            
        }
        $len = count($word_arr);
        for($i=0;$i<$len;$i++){
            for($j=$i+1;$j<$len;$j++){
                if(mb_strlen($word_arr[$i],'UTF8')<mb_strlen($word_arr[$j],'UTF8')){
                    $tmp=$word_arr[$j];
                    $word_arr[$j]=$word_arr[$i];
                    $word_arr[$i]=$tmp;
                }
            }
        }
    
        return $word_arr;
}




?>
